<?php
	add_action('widgets_init', 'register_great_deals_wg');
	function register_great_deals_wg()
	{
		register_widget('great_deals');
	}

	class great_deals extends WP_Widget // widget class
	{
		function great_deals()// widget setting | class name must be same function name
		{ 
			$widget_ops = array('classname' => 'great-deals-w', 'description' => 'Display deals from random stores');
			$control_ops = array(
				'width' => 250,
				'height' => 350,
				'id_base' => 'great_deals');
			$this->WP_Widget('great_deals', 'DealX - Great Deals', $widget_ops, $control_ops);
		}

	function widget($args, $instance)// display widget
	{
            $title = $instance['title'];
            $count_show = $instance['count_show'];
            $today = date('Y/m/d');
            $args = array(
                'post_type' => 'post',
                'post_status' => 'publish',
                'posts_per_page' => $count_show,
                'orderby' => 'meta_value',
                'meta_key' => 'start_date_metadata',
                'order' => 'ASC',
                'post__not_in' => get_option( 'sticky_posts' ),
                'meta_query' => array(
                    'relation' => 'AND',
                    array(
                        'key' => 'start_date_metadata',
                        'value' => $today,
                        'compare' => '<='
                    ),
                    array(
                        'key' => 'end_date_metadata',
                        'value' => $today,
                        'compare' => '>='
                    )
                )
            );
            $query = new WP_Query($args);
            $post = $query->posts;
            if($post){ ?>
            <div class="right-sibar-wrap">
                <div class="right-sidebar-title">
                    <span><?php echo $title; ?></span>
                    <a href="<?php echo site_url('/all-deals'); ?>" title="See all related deals">See All</a>
                    <div class="clr"></div>
                </div>
                <div class="all-deals-list row-fluid">
            <?php 
                    foreach ($post as $p){
                        cpx_get_deal_info($p->ID);
                    }?>
                </div>
            </div>
            <?php }
	}
	function update($new_instance, $old_instance)// update widget
	{
        $instance = $old_instance;
        $instance['title'] = $new_instance['title'];
        $instance['count_show'] = $new_instance['count_show'];
        return $instance;
	}

	function form($instance)
	{
	   ?>
         <div style="color: #333;">
    		<p>
                    <label for="<?php echo $this->get_field_id('title');?>"><?php echo 'Title'; ?></label>
                    <input type="text" id="<?php echo $this->get_field_id('title'); ?>" name="<?php	echo $this->get_field_name('title');?>" value="<?php echo $instance['title'];?>" style="width:100%;" />
    		</p>
                <p>
                    <label for="<?php echo $this->get_field_id('count_show');?>"><?php echo 'Enter number deals will display ';?></label>
                    <input type="text" id="<?php echo $this->get_field_id('count_show');?>" name="<?php echo $this->get_field_name('count_show');?>" value="<?php echo $instance['count_show'];?>" style="width:90%;" placeholder="Default display 2 deals" />
                </p>
        </div>
    <?php
	}
}
?>
